package org.emavaj.common;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import org.emavaj.sanguosha.model.constants.Constants;

/**
 * 
 * eMavaj updated 20120914
 *
 */
public class AudioUtil {

	public static Map<String, FileInputStream> cache
			= new HashMap<String, FileInputStream>();

//	public static Thread play(final String filename) {
//		Thread t = new Thread(new Runnable() {
//			public void run() {
//				try {
//					String path = Constants.AUDIO_DIR + Constants.FILE_SEP + filename;
//					FileInputStream stream = null;
//					if(cache.containsKey(path)) {
//						stream = cache.get(path);
//					} else {
//						stream = new FileInputStream(new File(path));
//					}
//
//					new Player(stream).play();
//				}catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//		t.start();
//		return t;
//	}
	
	public static Thread play(final String filename) {
		String path = Constants.AUDIO_DIR + Constants.FILE_SEP + filename;
		FileInputStream stream = null;
		if (cache.containsKey(path)) {
			stream = cache.get(path);
		} else {
			try {
				stream = new FileInputStream(new File(path));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		AudioThread t = new AudioThread(stream, false);
		t.start();
		return t;
	}
}